package com.jrx.anytxn.transaction.job.fileRepay.step;

import com.jrx.anytxn.transaction.bean.FilePaymentSaveBean;
import com.jrx.anytxn.transaction.bean.PaymentSaveBean;
import com.jrx.anytxn.transaction.config.InterestFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.TlMistakeLog;
import com.jrx.anytxn.transaction.entity.TlThirdPartyPayment;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlMistakeLogMapper;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlThirdPartyPaymentMapper;
import com.jrx.anytxn.transaction.service.IDealPaymentService;
import com.jrx.anytxn.transaction.service.IInterestFileService;
import com.jrx.anytxn.transaction.service.IMistakeService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class FilePayWriter implements ItemWriter<FilePaymentSaveBean> {

    @Autowired
    private IDealPaymentService dealPaymentService;


    @Autowired
    private IMistakeService mistakeService;

    @Resource
    private ExtTlThirdPartyPaymentMapper extTlThirdPartyPaymentMapper;


    @Override
    public void write(List<? extends FilePaymentSaveBean> items) throws Exception {
        List<TlThirdPartyPayment> thirdPartyPaymentList = new ArrayList<>();
        List<TlMistakeLog> tlMistakeLogList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(items)){
            for(FilePaymentSaveBean saveBean : items) {
                if(null != saveBean.getPaymentSaveBean()) {
                    dealPaymentService.saveAllAccts(saveBean.getPaymentSaveBean());
                }

                if(null != saveBean.getMistakeLog()){
                    tlMistakeLogList.add(saveBean.getMistakeLog());
                }

                if(null != saveBean.getTlThirdPartyPayment()){
                    thirdPartyPaymentList.add(saveBean.getTlThirdPartyPayment());
                }
            }
        }

        if(CollectionUtils.isNotEmpty(thirdPartyPaymentList)){
            extTlThirdPartyPaymentMapper.batchUpdateLogForBatch(thirdPartyPaymentList);
        }

        if(CollectionUtils.isNotEmpty(tlMistakeLogList)){
            mistakeService.batchInsert(tlMistakeLogList);
        }

    }
}
